# -*- coding: utf-8 -*-

import hashlib
from base import UserCase, Runner, TestSuite


class Case(UserCase):

    def test_my_corp_list(self):
        p = {
            'name': self.randhan(8),
            'username': self.randchar(8),
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
        }
        res = self.c.open('/api/corp/create', p)
        self.assertEqual(res['code'], 0)

        res = self.c.open('/login', {'username': p['username'], 'password': p['password']})
        self.assertEqual(res['code'], 0)


        res = self.c.open('/api/my-corp/list')
        self.assertEqual(res['code'], 0)


    def test_my_corp_read(self):
        p = {
            'name': self.randhan(8),
            'username': self.randchar(8),
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
        }
        res = self.c.open('/api/corp/create', p)
        self.assertEqual(res['code'], 0)

        res = self.c.open('/login', {'username': p['username'], 'password': p['password']})
        self.assertEqual(res['code'], 0)


        res = self.c.open('/api/my-corp/read')
        self.assertEqual(res['code'], 0)
        self.assertEqual(res['data']['name'], p['name'])


    def test_my_corp_update(self):
        p = {
            'name': self.randhan(8),
            'username': self.randchar(8),
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
        }
        res = self.c.open('/api/corp/create', p)
        self.assertEqual(res['code'], 0)

        res = self.c.open('/login', {'username': p['username'], 'password': p['password']})
        self.assertEqual(res['code'], 0)


        res = self.c.open('/api/my-corp/read')
        self.assertEqual(res['code'], 0)
        corp = res['data']['id']

        name = self.randhan(8)
        contacts = self.randhan(3)
        tel = self.randnum(6)
        res = self.c.open('/api/my-corp/update', {'name': name, 'contacts': contacts, 'tel': tel})
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/my-corp/read')
        self.assertEqual(res['data']['name'], name)
        self.assertEqual(res['data']['contacts'], contacts)
        self.assertEqual(res['data']['tel'], tel)


    def test_my_corp_read_as_normal(self):

        p = {
            'name': self.randhan(8),
            'username': self.randchar(8),
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
        }
        res = self.c.open('/api/corp/create', p)
        self.assertEqual(res['code'], 0)

        res = self.c.open('/login', {'username': p['username'], 'password': p['password']})
        self.assertEqual(res['code'], 0)

        p = {
            'name': self.randhan(3),
            'role': 'normal',
            'password': hashlib.md5(self.randchar(6)).hexdigest(),
            'username': self.randchar(8),
        }
        res = self.c.open('/api/my-corp-user/create', p)
        self.assertEqual(res['code'], 0)

        res = self.c.open('/login', {'username': p['username'], 'password': p['password']})
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/my-corp/read')
        self.assertNotEqual(res['code'], 0)

test_list = [
    'test_my_corp_list',
    'test_my_corp_read',
    'test_my_corp_update',
    'test_my_corp_read_as_normal',
]
Suite = TestSuite([Case(t) for t in test_list])


if __name__ == '__main__':
    Runner.run(Suite)



